package com.thread;

/**
 * @author :Administrator
 * @description: Join() 方法  调用线程将被阻塞，直到被join()方法加入的join线程执行完为止
 * 本示例：让线程按顺序执行
 * @create date: 2023/2/19 17:52
 */
public class OrderJoinThread {
    public static void main(String[] args) {
        Thread t1=new Thread(() -> {
            try {
                for (int i = 0; i < 5; i++) {
                    Thread.sleep(1000);
                    System.out.println("name:"+Thread.currentThread().getName()+" i="+i);
                }
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        },"t1");
        Thread t2=new Thread(() -> {
            try {
                //在t2线程中调用，t2为调用线程将被阻塞，直到被join()方法加入的join线程执行完为止
                t1.join();
                for (int i = 0; i < 5; i++) {
                    Thread.sleep(1000);
                    System.out.println("name:"+Thread.currentThread().getName()+" i="+i);
                }

            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        },"t2");
        Thread t3=new Thread(() -> {
            try {
                t2.join();
                for (int i = 0; i < 5; i++) {
                    Thread.sleep(1000);
                    System.out.println("name:"+Thread.currentThread().getName()+" i="+i);
                }

            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        },"t3");
        t1.start();
        t2.start();
        t3.start();

    }
}
